package com.rongji.cms.tools.server.runner;


import com.rongji.cms.tools.common.constant.CacheNames;
import com.rongji.cms.tools.common.utils.JsonUtils;
import com.rongji.cms.tools.common.utils.redis.CacheUtils;
import com.rongji.cms.tools.common.utils.redis.RedisUtils;
import com.rongji.cms.tools.file.constant.FileConstant;
import com.rongji.cms.tools.file.oss.constant.OssConstant;
import com.rongji.cms.tools.file.oss.factory.OssFactory;
import com.rongji.cms.tools.file.oss.properties.OssProperties;
import com.rongji.cms.tools.file.service.ToolsOssService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * @author yang
 * @date 2022/11/21 13:03
 * @description 系统启动后一些初始化
 */
@Slf4j
@RequiredArgsConstructor
@Component
public class InitApplicationRunner implements ApplicationRunner {


    @Autowired
    private ToolsOssService toolsOssService;
    @Autowired
    private OssProperties ossProperties;

    @Override
    public void run(ApplicationArguments applicationArguments) {
//        每次重启把不会过期的缓存清掉
        CacheUtils.evict(CacheNames.ALL_ATTACHMENT_CACHENAME, FileConstant.ALL_ATTACHMENT_ID);
//       ------------------------------------------------
//        fixme 先临时这样，等后面需要做多种oss时候再在oss配置里做这些操作
//        1、订阅oss配置；2、给个初始值，目前就是minio，3、发布到redis里
        OssFactory.init();
        RedisUtils.setCacheObject(OssConstant.DEFAULT_CONFIG_KEY, FileConstant.SERVICE_MINIO);
        CacheUtils.put(CacheNames.SYS_OSS_CONFIG, FileConstant.SERVICE_MINIO, JsonUtils.toJsonString(ossProperties));
        RedisUtils.publish(OssConstant.DEFAULT_CONFIG_KEY, FileConstant.SERVICE_MINIO, msg -> {
            log.info("发布刷新OSS配置 => " + msg);
        });
//        --------------------------------------
        toolsOssService.init();
        log.info("-------------oss已初始化!!!");

    }
}
